﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class NeedMaterialListForStockOutNoteHeader : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
               // UART.BLL.Sales.NeedMaterialHeader bll = new UART.BLL.Sales.NeedMaterialHeader();
                //设置经办人
                Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j => j.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                //只取出StatusSubmit为2 即设计师已经提交到财务的订单列表
                this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text + " and StatusSubmit='2'";
               // this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
                this.BindData();
                //设置分页页数
                this.txtPageSize.Attributes.Add("onkeydown", "javascript:SetFocusPageSize();");

            }
        }

        /// <summary>
        /// 绑定订单列表
        /// </summary>
        private void BindData()
        {
         //   UART.BLL.Sales.NeedMaterialHeader bll = new UART.BLL.Sales.NeedMaterialHeader();
          var ds = mk.l.Sales_NeedMaterialHeader.ToList();
            this.myGridView.DataSource = ds;
            this.myGridView.DataBind();
        }

        /// <summary>
        /// 分页绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        /// <summary>
        /// 绑定数据时显示状态信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
      var bllNeedHeader = mk.l.Sales_NeedMaterialHeader.ToList();
          var bllOrderHeader =mk.l.Sales_SalesOrderHeader.ToList();
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //取得订货单信息
             Sales_NeedMaterialHeader mNeedHeader = bllNeedHeader.Where(h=>h.NeedMaterialID==int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["NeedMaterialID"].ToString())).FirstOrDefault();
                //取得订单信息
                       Sales_SalesOrderHeader mOrderHeader = bllOrderHeader.Where(h=>h.SalesOrderID==mNeedHeader.SalesOrderID).FirstOrDefault();

                //显示客户类型
                Label lblCustomerName = (Label)(e.Row.FindControl("lblCustomerName"));
               var bllC = mk.l.Sales_Customer.ToList();
           Sales_Customer mC = bllC.Where(h=>h.CustomerID==mOrderHeader.CustomerID).FirstOrDefault();
                if (mC != null)
                {
                    lblCustomerName.Text = mC.CustomerName;
                }
                //显示订单编号
                Label lblSalesOrderNumber = (Label)(e.Row.FindControl("lblSalesOrderNumber"));
                lblSalesOrderNumber.Text = mOrderHeader.SalesOrderNumber;
                //显示设计师姓名
                Label lblDesigner = (Label)(e.Row.FindControl("lblDesigner"));
              var bllUser = mk.l.Accounts_Users.ToList();

    Accounts_Users mUser = bllUser.Where(h=>h.UserName==mOrderHeader.Designer).FirstOrDefault();
                lblDesigner.Text = mUser.TrueName;

                //显示提交状态
                Label lblStatusSubmit = (Label)(e.Row.FindControl("lblStatusSubmit"));
                string strStatusSubmit = mNeedHeader.StatusSubmit;
                switch (strStatusSubmit)
                {
                    case "1":
                        lblStatusSubmit.Text = "初始添加";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusSubmit.Text = "已提交";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusSubmit.Text = "未知";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                ////显示付款状态
                //Label lblStatusPayment = (Label)(e.Row.FindControl("lblStatusPayment"));
                //string strStatusPayment = this.myGridView.DataKeys[e.Row.RowIndex].Values["StatusPayment"].ToString();
                //switch (strStatusPayment)
                //{
                //    case "1":
                //        lblStatusPayment.Text = "未付款";
                //        lblStatusPayment.ForeColor = System.Drawing.Color.Red;
                //        break;
                //    case "2":
                //        lblStatusPayment.Text = "未付全款";
                //        lblStatusPayment.ForeColor = System.Drawing.Color.Black;
                //        break;
                //    case "3":
                //        lblStatusPayment.Text = "已付全款";
                //        lblStatusPayment.ForeColor = System.Drawing.Color.Green;
                //        break;
                //    default:
                //        lblStatusPayment.Text = "未知";
                //        lblStatusPayment.ForeColor = System.Drawing.Color.Red;
                //        break;
                //}

                //显示订货状态 同时根据订货状态 确定编辑链接是Creat还是Edit
                Label lblStatusPurchaseMaterial = (Label)(e.Row.FindControl("lblStatusPurchaseMaterial"));
                //HyperLink linkPurchaseOrderEdit = (HyperLink)e.Row.FindControl("linkPurchaseOrderEdit");
                string strStatusPurchaseMaterial = mNeedHeader.StatusPurchaseMaterial;
                //string NeedMaterialID = this.myGridView.DataKeys[e.Row.RowIndex].Values["NeedMaterialID"].ToString();
                switch (strStatusPurchaseMaterial)
                {
                    case "1":
                        lblStatusPurchaseMaterial.Text = "未订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusPurchaseMaterial.Text = "已订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusPurchaseMaterial.Text = "未知";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //显示出库状态
                Label lblStatusStockOut = (Label)(e.Row.FindControl("lblStatusStockOut"));
                string strStatusStockOut = mNeedHeader.StatusStockOut;
                switch (strStatusStockOut)
                {
                    case "1":
                        lblStatusStockOut.Text = "未出库";
                        lblStatusStockOut.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "2":
                        lblStatusStockOut.Text = "已出库";
                        lblStatusStockOut.ForeColor = System.Drawing.Color.Green;
                        break;
                    default:
                        lblStatusStockOut.Text = "未知";
                        lblStatusStockOut.ForeColor = System.Drawing.Color.Red;
                        break;
                }

                //让数字以文本形式表示
                //e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            }
        }

        /// <summary>
        /// Handles the Click event of the btnSetPageSize control.设置分页页数
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSetPageSize_Click(object sender, EventArgs e)
        {
            if (this.txtPageSize.Text.Trim() != "")
            {
                if (this.txtPageSize.Text.Trim() != "0")
                {
                //    this.AspNetPager.PageSize = int.Parse(this.txtPageSize.Text.Trim());
                    this.BindData();
                }
                else
                {
                  //  this.AspNetPager.PageSize = 10;
                    this.BindData();
                }
            }
            else
            {
              //  this.AspNetPager.PageSize = 10;
                this.BindData();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSearch control.查询相关订单信息
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text + " and StatusSubmit='2'";
            //设置查询条件
            //所有的条件都加and 最后再去掉就行了 tnnd早想到这个办法多好啊 不用写那么多if了
            StringBuilder strWhereCustomer = new StringBuilder();
            if (this.txtCustomerName.Text.Trim() != "")
            {
                strWhereCustomer.AppendFormat(" and CustomerName like '%{0}%'", mk.InputText(this.txtCustomerName.Text, 50));
            }
            if (this.txtPhone.Text.Trim() != "")
            {
                strWhereCustomer.AppendFormat(" and Phone like '%{0}%'", mk.InputText(this.txtPhone.Text, 20));
            }
            if (this.txtCellPhone.Text.Trim() != "")
            {
                strWhereCustomer.AppendFormat(" and CellPhone like '%{0}%'", mk.InputText(this.txtCellPhone.Text, 20));
            }
            if (this.txtAddress.Text.Trim() != "")
            {
                strWhereCustomer.AppendFormat(" and Address like '%{0}%'", mk.InputText(this.txtAddress.Text, 100));
            }

            StringBuilder strWhereOrder = new StringBuilder();


            if (this.txtSalesOrderNumber.Text.Trim() != "")
            {
                strWhereOrder.AppendFormat(" and SalesOrderNumber like '%{0}%'", mk.InputText(this.txtSalesOrderNumber.Text, 30));
            }

            StringBuilder strWhereAll = new StringBuilder();
            if (strWhereOrder.Length > 0)
            {
                strWhereAll.Append(strWhereOrder);
            }
            if (strWhereCustomer.Length > 0)
            {
                strWhereAll.Append(" and CustomerID in (select CustomerID from Sales_Customer where ");
                strWhereAll.Append(strWhereCustomer.ToString().Substring(5, strWhereCustomer.Length - 5));
                strWhereAll.Append(")");
            }
            StringBuilder strWhere = new StringBuilder();
            if (strWhereAll.Length > 0)
            {
                strWhere.Append(" and SalesOrderID in (select SalesOrderID from Sales_SalesOrderHeader where ");
                strWhere.Append(strWhereAll.ToString().Substring(5, strWhereAll.Length - 5));
                strWhere.Append(")");
            }
            if (strWhere.Length > 0)
            {
                //this.lblStrWhere.Text = strWhere3.ToString().Substring(5, strWhere3.Length - 5);
                this.lblStrWhere.Text += strWhere.ToString();
            }
          //  UART.BLL.Sales.NeedMaterialHeader bll = new UART.BLL.Sales.NeedMaterialHeader();
          //  this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
            this.BindData();
        }

    }
}
